import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-09-08
 * Time: 10:05
 */
public class MyThreadPool {
    //线程池的目的,就是为了多个完成任务
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);

    public MyThreadPool(int n) throws InterruptedException {
        //创建n个线程
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                while (true) {
                    try {
                        Runnable runnable = queue.take();//从队列取出任务
                        runnable.run();//执行任务
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();//启动线程
        }
    }


    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
